#ifndef AREA_READER_H
#define AREA_READER_H

#include "reader.h"
#include <vector>
#include <map>
#include <sstream>

#define NUM_AREA_SIZE sizeof(int)
#define	AREA_REGION_NAME_SIZE	(sizeof(char) * 20)
#define AREA_NAME_SIZE (sizeof(char) * 20)
#define AREA_INDEX_SIZE sizeof(long)
#define AREA_INFO_HEADER_SIZE (AREA_REGION_NAME_SIZE+AREA_NAME_SIZE+AREA_INDEX_SIZE)

#define NUM_AREA_OFFSET 6 * sizeof(int)
#define AREA_INFO_HEADER_OFFSET (NUM_AREA_OFFSET + NUM_AREA_SIZE)
#define MAX_AREA_VAR 26

#define NUM_SUMMARY_DATA 15

#define GET_AREA_VAR_SQL  "SELECT VALUE FROM SIMULATIONCONTROL WHERE NAME='Hour Var Selection ARZ'"

#define GET_AREA_ANC_INDEX_SQL "SELECT VALUE FROM SIMULATIONCONTROL WHERE NAME='Ancillary Service Type Index'"

#define GET_AREA_EMISSION_COUNT_SQL "SELECT COUNT(MONITOR)AS NUM_EMI FROM EMISSIONTYPE WHERE MONITOR=-1"

#define GET_AREA_EMISSION_NAME_SQL "SELECT NAME FROM EMISSIONTYPE WHERE MONITOR=-1" //Yanli Wei, 06/10/2010

#define GET_AREA_EMISSION_SETTING_SQL "SELECT VALUE FROM SIMULATIONCONTROL WHERE NAME='Emission Model Option'"


typedef struct _area_info 
{
	long area_index;
	char AreaName[20];
	char RegionName[20];
}area_info;

// Reorderd annd add last three variables to match the OUTPUT binary file, Yanli Wei, 06/14/2010
typedef struct _area_yearly_data
{
	float Avg_LMP;
	float Avg_LMP_Congestion;
	float Avg_LMP_Loss;
	float Tot_Generation;
	float Tot_Load;
	float Tot_ServedLoad;
	float Tot_UnservedLoad;
	float Tot_Pumping;
	float Tot_LoadPayment;
	float Tot_Revenue;
	float Tot_GenCost;
	float Tot_UnservedCost;
	float Tot_CongDemandPay;
	float Tot_LossDemandPay;
	float Tot_CongSupplyRev;	
}area_yearly_data;

/*
typedef struct _stats
{
	float total;
	float avg;
	float min;
	float max;
}stats;
*/

class area_reader: public reader
{

protected:

	int summary_file_Handle;					// file handlder for summary binary file
	long hourly_num_area;						// total number of area store in the hourly binary file
	CString area_var_setting;					// area variable output bit setting
	CString area_anc_setting;					// area Ancillary Service setting
	CString area_emssions_count;				// area emssions Count setting
	CString area_emssions_option;				// area option setting

	map <CString, int> hourly_area_index_map;	// hourly area' name and index map
	map <CString, int> var_index_map;			// hourly area varables name and index map

	/* read area varables setting from MS DB*/
	void read_area_var_setting(CString * area_var_setting);
	/* read area info from binary file (hourly, summary)*/
	void read_area_info(int f_handler, long * num, vector<area_info> * info_list, map <CString, int> * info_map);
	/* read number of area from binary file (hourly, summary)*/
	void read_area_num(int f_handle, long * num);
	/* read emission types */
	void readEmissionType(); // Yanli Wei, 06/10/2010


public:

	int num_ancs;								// total number of Ancillary Service
	int num_emissions;							// total number of emissions
	int num_vars;								// total number of selected output area variables
	int emissions_option;                       // Emission option, Yanli Wei, 06/09/2010

	vector<char *> area_var_list;				// area variables list
	vector<area_info> hourly_area_info_list;	// area information list 
	vector<CString> area_type_list;				// area types and index map
	map <CString, int> area_type_map;			// area types and index map

	vector<CString> emission_type_list;         // Emission type list, Yanli Wei, 06/10/2010     
	vector<CString> AS_type_list;				// Ancillary service type list, Yanli Wei, 06/10/2010

	/* constructors and deconstructor */
	area_reader();
	area_reader(char *HourlyBinFile, char *SummaryBinFile, char *dbFile);
	~area_reader();

	char ** get_area_var_list();
	int get_var_list_size();
	int get_area_info_list_size();
	int get_area_var_list_size();
	int get_area_index(char *, map <CString, int> * );
	long get_num_area();
	unsigned long get_hourly_var_offset(int area_index, unsigned long hour, int var_index);
	unsigned long get_summary_data_offset(int area_index);

	float get_area_var(char* AreaName, char* RegionName, char * var_name, unsigned long hour);
	float * getAllHourlyVal(char* AreaName, char* RegionName, unsigned long area_hrs);
	float getCommitCode(char* AreaName, char* RegionName, int hour);

	area_info * get_area_info_list();
	area_yearly_data get_area_yearly_data(CString AreaName, CString RegionName);

	void readAncSetting(CString);
	void readEmssionsOption(CString area_emssions_option); // Yanli Wei, 06/10/2010
	void readEmssionsCount(CString area_emssions_count); // Yanli Wei, 06/10/2010

};

#endif